#include "GameScene.h"
#include "SimpleAudioEngine.h"
#include "WelcomeScene.h"
#include "ControlLayer.h"
using namespace cocos2d;
using namespace CocosDenshion;

CCScene* GameScene::scene() {
	CCLog("started");

	CCScene *scene = CCScene::create();
	GameScene *layer = GameScene::create();
	scene->addChild(layer);
	return scene;
}

bool GameScene::init() {
	if (!CCLayer::init()) {
		return false;
	}

	BackGroundLayer *bckgrd = BackGroundLayer::node();
	this->addChild(bckgrd,0);
	ControlLayer *cL=ControlLayer::node();
	this->addChild(cL,2);
	ScoreLayer *scrL=ScoreLayer::node();
	this->addChild(scrL,2);

	GameplayLayer *gpl = GameplayLayer::node();
	gpl->connectControlsWithJoystick(cL->leftJoystick,cL->jumpButton);
	gpl->setScore(scrL);
	this->addChild(gpl,1);

	renderMenu();
	return true;
}

void GameScene::renderMenu() {
	CCMenuItemImage *pCloseItem = CCMenuItemImage::create("btn_back.png",
			"btn_back.png", this, menu_selector(GameScene::goBack));
	CCSize size = CCDirector::sharedDirector()->getWinSize();
	pCloseItem->setPosition(ccp(size.width - 40, size.height-40));
	CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
	pMenu->setPosition(CCPointZero);
	this->addChild(pMenu, 1);

}

void GameScene::goBack(CCObject* sender) {
	CCScene *pScene = Welcome::scene();
	CCDirector::sharedDirector()->replaceScene(pScene);
}
